<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    data: {
      type: Array,
      default: [
        { name: "自动", value: 1 },
        { name: "主动", value: 1 },
      ],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    data(val, oldVal) {
      //普通的watch监听
      // this.initChart();
      this.getData();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
      this.getData();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      var option = {
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          left: "center",
          bottom: "10",
          // data: ["Industries", "Technology", "Forex", "Gold", "Forecasts"],
        },
        series: [
          {
            name: "SUMMARY",
            type: "pie",
            roseType: "radius", // 显示 南丁格尔玫瑰图
            radius: [15, 95],
            center: ["50%", "38%"],
            animationEasing: "cubicInOut",
            animationDuration: 2000,
          },
        ],
      };
      this.chart.setOption(option);
    },

    getData() {
      var option = {
        series: [
          {
            data: this.data,
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>
